<template>
  <div class="code-result">
    <div class="result-header">
      <div class="status-info">
        <el-tag :type="getStatusType(result.result)">{{ getStatusText(result.result) }}</el-tag>
        <div v-if="result.time_used" class="metrics">
          <span class="time">执行时间: {{ result.time_used }}ms</span>
          <span class="memory">内存: {{ formatMemory(result.memory_used) }}</span>
        </div>
      </div>
      <div class="test-info" v-if="result.total_test_cases">
        <span>通过测试: {{ result.test_cases_passed }}/{{ result.total_test_cases }}</span>
      </div>
    </div>

    <div v-if="result.compile_output" class="output-section">
      <div class="section-title">编译/运行输出:</div>
      <pre class="output-content"><code>{{ result.compile_output }}</code></pre>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import type { SubmissionResult } from '../api/code'

const props = defineProps<{
  result: SubmissionResult
}>()

const getStatusText = (status: number) => {
  const statusMap = {
    0: '待评测',
    1: '评测中',
    2: '通过',
    3: '答案错误',
    4: '编译错误',
    5: '运行时错误',
    6: '时间超限'
  }
  return statusMap[status] || '未知状态'
}

const getStatusType = (status: number) => {
  if (status === 2) return 'success'
  if (status === 0 || status === 1) return 'info'
  return 'error'
}

const formatMemory = (memory?: number) => {
  if (!memory) return '0 KB'
  if (memory < 1024) return `${memory} KB`
  return `${(memory / 1024).toFixed(2)} MB`
}
</script>

<style scoped>
.code-result {
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.status-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.metrics {
  display: flex;
  gap: 16px;
  color: #666;
  font-size: 14px;
}

.test-info {
  font-size: 14px;
  color: #666;
}

.output-section {
  background: #fff;
  border-radius: 6px;
  padding: 16px;
  margin-top: 16px;
}

.section-title {
  font-weight: 500;
  margin-bottom: 8px;
  color: #333;
}

.output-content {
  background: #f1f3f5;
  padding: 12px;
  border-radius: 4px;
  margin: 0;
  white-space: pre-wrap;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 14px;
  line-height: 1.5;
  color: #333;
}
</style>
